package com.ruoyi.subject.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.subject.mapper.SubjectTaskMapper;
import com.ruoyi.subject.domain.SubjectTask;
import com.ruoyi.subject.service.ISubjectTaskService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.util.CollectUtil;

/**
 * 课题项目Service业务层处理
 * 
 * @author yepanpan
 * @date 2021-03-15
 */
@Service
public class SubjectTaskServiceImpl implements ISubjectTaskService 
{
    @Autowired
    private SubjectTaskMapper subjectTaskMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询课题项目
     * 
     * @param id 课题项目ID
     * @return 课题项目
     */
    @Override
    public SubjectTask selectSubjectTaskById(Long id)
    {
    	SubjectTask subjectTask = subjectTaskMapper.selectSubjectTaskById(id);
		if(!StringUtils.isEmpty(subjectTask.getUserIds())) {
			String userIds = subjectTask.getUserIds();
	    	userIds = userIds.substring(1,userIds.length()-1);
	    	String[] usersId = userIds.split(",");
	    	Long userId[] = new Long[usersId.length];
	    	String userNames = "";
	    	for(int i=0;i<usersId.length;i++) {
	    		userId[i] = Long.parseLong(usersId[i]);
	    		SysUser user = sysUserMapper.selectUserById(userId[i]);
	    		if(user != null) {
	    			userNames += ","+user.getNickName();
	    		}
	    	}
	    	subjectTask.setUserNames(userNames.substring(1));
	    	subjectTask.setUserId(userId);
		}
    	
    	return subjectTask;
    }

    /**
     * 查询课题项目列表
     * 
     * @param subjectTask 课题项目
     * @return 课题项目
     */
    @Override
    public List<SubjectTask> selectSubjectTaskList(SubjectTask subjectTask)
    {
    	List<SubjectTask> list = subjectTaskMapper.selectSubjectTaskList(subjectTask);
    	for(SubjectTask row : list) {
    		if(StringUtils.isEmpty(row.getUserIds())) break;
    		
    		String userIds = row.getUserIds();
        	userIds = userIds.substring(1,userIds.length()-1);
        	String[] usersId = userIds.split(",");
        	Long userId[] = new Long[usersId.length];
        	String userNames = "";
        	for(int i=0;i<usersId.length;i++) {
        		userId[i] = Long.parseLong(usersId[i]);
        		SysUser user = sysUserMapper.selectUserById(userId[i]);
        		if(user != null) {
        			userNames += ","+user.getNickName();
        		}
        	}
        	row.setUserNames(userNames.substring(1));
        	row.setUserId(userId);
    	}
    	return list;
    }

    /**
     * 新增课题项目
     * 
     * @param subjectTask 课题项目
     * @return 结果
     */
    @Override
    public int insertSubjectTask(SubjectTask subjectTask)
    {
    	if(subjectTask.getMasterUserId() == null) {
    		subjectTask.setMasterUserId(SecurityUtils.getLoginUser().getUser().getUserId());
    	}
    	if(subjectTask.getUserId() != null) {
    		subjectTask.setUserIds(CollectUtil.joins(subjectTask.getUserId(), ","));
		}
    	subjectTask.setProgress("1");
    	subjectTask.setProgressStatus("0");
        subjectTask.setCreateTime(DateUtils.getNowDate());
        subjectTask.setCreateBy(SecurityUtils.getUsername());
        return subjectTaskMapper.insertSubjectTask(subjectTask);
    }

    /**
     * 修改课题项目
     * 
     * @param subjectTask 课题项目
     * @return 结果
     */
    @Override
    public int updateSubjectTask(SubjectTask subjectTask)
    {
    	if(subjectTask.getUserId() != null) {
    		subjectTask.setUserIds(CollectUtil.joins(subjectTask.getUserId(), ","));
		}
        subjectTask.setUpdateTime(DateUtils.getNowDate());
        subjectTask.setUpdateBy(SecurityUtils.getUsername());
        return subjectTaskMapper.updateSubjectTask(subjectTask);
    }

    /**
     * 批量删除课题项目
     * 
     * @param ids 需要删除的课题项目ID
     * @return 结果
     */
    @Override
    public int deleteSubjectTaskByIds(Long[] ids)
    {
        return subjectTaskMapper.deleteSubjectTaskByIds(ids);
    }

    /**
     * 删除课题项目信息
     * 
     * @param id 课题项目ID
     * @return 结果
     */
    @Override
    public int deleteSubjectTaskById(Long id)
    {
        return subjectTaskMapper.deleteSubjectTaskById(id);
    }
}
